package db;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class CodeInput extends JPanel {

	static Connection con;
	DefaultTableModel model;
	private JLabel label;
	private JPanel panel_1;
	private JTextField text1;
	private JLabel lblNewLabel;
	private JPanel panel_2;
	private JLabel label_1;
	private JPanel panel_3;
	private JTextField text2;
	private JLabel label_3;
	private JPanel panel_5;
	private JTextField text3;
	private JLabel label_4;
	private JPanel panel_6;
	private JTextField text4;
	private JLabel label_5;
	private JPanel panel_7;
	private JTextField text5;
	private JSeparator separator_6;
	private JPanel panel_8;
	private JPanel panel_9;
	private JPanel panel_11;
	private JPanel panel_12;
	private JPanel panel_13;
	private JPanel panel_14;
	private JPanel panel_15;
	private JPanel panel;
	private JScrollPane scrollPane;
	private JTable table;
	Vector<String> col = new Vector<String>();
	private JTextField text;
	private JButton button;
	private JSeparator separator;
	private JPanel panel_4;
	private JPanel panel_10;
	private JLabel lblNewLabel_1;
	private JLabel label_2;
	private JButton button_1;

	public CodeInput(Connection con) {
		this.con = con;
		col.add("거래처");
		col.add("사업자 번호");
		model = new DefaultTableModel(col, 0);
		setLayout(new BorderLayout(0, 0));
		add(getLabel(), BorderLayout.NORTH);
		add(getPanel(), BorderLayout.EAST);
		add(getPanel_1(), BorderLayout.CENTER);
		add(getPanel_15(), BorderLayout.WEST);
		search();
		DefaultTableCellRenderer Center = new DefaultTableCellRenderer();
		Center.setHorizontalAlignment(SwingConstants.CENTER);// 가운데 정렬
		TableColumnModel tcmSchedule = table.getColumnModel();
		tcmSchedule.getColumn(0).setCellRenderer(Center);
		tcmSchedule.getColumn(1).setCellRenderer(Center);
	}

	private boolean search() {
		try {
			model.setRowCount(0);
			Statement stmt = con.createStatement();
			String sql = "select name,num from customer";
			ResultSet RS = stmt.executeQuery(sql);
			while (RS.next()) {
				Vector<String> row = new Vector<String>();
				row.add(RS.getString(1));
				row.add(RS.getString(2));
				model.addRow(row);
			}
			RS.close();
			stmt.close();

			return true;
		} catch (Exception e) {
			System.out.println("예외사항:" + e.getMessage());
			return false;
		}
	}

	private boolean search2() {
		try {
			String str = text.getText();
			model.setRowCount(0);
			Statement stmt = con.createStatement();
			String sql = "select name,num from customer where name like '%"
					+ str + "%' or num like '%" + str + "%'";
			ResultSet RS = stmt.executeQuery(sql);
			while (RS.next()) {
				Vector<String> row = new Vector<String>();
				row.add(RS.getString(1));
				row.add(RS.getString(2));
				model.addRow(row);
			}
			RS.close();
			stmt.close();

			return true;
		} catch (Exception e) {
			System.out.println("예외사항:" + e.getMessage());
			return false;
		}
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("제품 입력");
			label.setForeground(Color.WHITE);
			label.setPreferredSize(new Dimension(50, 50));
			label.setBackground(Color.BLACK);
			label.setOpaque(true);
			label.setHorizontalAlignment(SwingConstants.CENTER);
			label.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 50));
		}
		return label;
	}

	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setBackground(Color.BLACK);
			panel_1.setPreferredSize(new Dimension(300, 10));
			panel_1.setMaximumSize(new Dimension(100, 32767));
			panel_1.setLayout(null);
			panel_1.add(getSeparator_6());
			panel_1.add(getPanel_4());
			panel_1.add(getPanel_8());
			panel_1.add(getLblNewLabel());
			panel_1.add(getPanel_2());
			panel_1.add(getText1());
			panel_1.add(getPanel_9());
			panel_1.add(getLabel_1());
			panel_1.add(getPanel_3());
			panel_1.add(getText2());
			panel_1.add(getPanel_11());
			panel_1.add(getLabel_3());
			panel_1.add(getPanel_5());
			panel_1.add(getText3());
			panel_1.add(getPanel_12());
			panel_1.add(getLabel_4());
			panel_1.add(getPanel_6());
			panel_1.add(getText4());
			panel_1.add(getPanel_13());
			panel_1.add(getLabel_5());
			panel_1.add(getPanel_7());
			panel_1.add(getText5());
			panel_1.add(getPanel_14());
			panel_1.add(getButton_1());
			panel_1.add(getLblNewLabel_1());
		}
		return panel_1;
	}

	private JTextField getText1() {
		if (text1 == null) {
			text1 = new JTextField();
			text1.setBounds(135, 30, 204, 30);
			text1.setPreferredSize(new Dimension(230, 30));
			text1.setMinimumSize(new Dimension(230, 21));
			text1.setMaximumSize(new Dimension(3000000, 2147483647));
			text1.setColumns(18);
		}
		return text1;
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("제품 코드");
			lblNewLabel.setBounds(35, 30, 65, 30);
			lblNewLabel.setForeground(Color.WHITE);
			lblNewLabel.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 22));
			lblNewLabel.setPreferredSize(new Dimension(65, 30));
		}
		return lblNewLabel;
	}

	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
			panel_2.setBounds(105, 40, 25, 10);
			panel_2.setPreferredSize(new Dimension(25, 10));
		}
		return panel_2;
	}

	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("제품 이름");
			label_1.setBounds(35, 65, 65, 30);
			label_1.setForeground(Color.WHITE);
			label_1.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 22));
			label_1.setPreferredSize(new Dimension(65, 30));
		}
		return label_1;
	}

	private JPanel getPanel_3() {
		if (panel_3 == null) {
			panel_3 = new JPanel();
			panel_3.setBounds(105, 75, 25, 10);
			panel_3.setPreferredSize(new Dimension(25, 10));
		}
		return panel_3;
	}

	private JTextField getText2() {
		if (text2 == null) {
			text2 = new JTextField();
			text2.setBounds(135, 65, 204, 30);
			text2.setPreferredSize(new Dimension(230, 30));
			text2.setMinimumSize(new Dimension(230, 21));
			text2.setMaximumSize(new Dimension(3000000, 2147483647));
			text2.setColumns(18);
		}
		return text2;
	}

	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("제품 원가");
			label_3.setBounds(35, 100, 65, 30);
			label_3.setForeground(Color.WHITE);
			label_3.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 22));
			label_3.setPreferredSize(new Dimension(65, 30));
		}
		return label_3;
	}

	private JPanel getPanel_5() {
		if (panel_5 == null) {
			panel_5 = new JPanel();
			panel_5.setBounds(105, 110, 25, 10);
			panel_5.setPreferredSize(new Dimension(25, 10));
		}
		return panel_5;
	}

	private JTextField getText3() {
		if (text3 == null) {
			text3 = new JTextField();
			text3.setBounds(135, 100, 204, 30);
			text3.setPreferredSize(new Dimension(230, 30));
			text3.setMinimumSize(new Dimension(230, 21));
			text3.setMaximumSize(new Dimension(3000000, 2147483647));
			text3.setColumns(18);
		}
		return text3;
	}

	private JLabel getLabel_4() {
		if (label_4 == null) {
			label_4 = new JLabel("받은놈");
			label_4.setBounds(35, 135, 65, 30);
			label_4.setForeground(Color.WHITE);
			label_4.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 22));
			label_4.setHorizontalAlignment(SwingConstants.CENTER);
			label_4.setPreferredSize(new Dimension(65, 30));
		}
		return label_4;
	}

	private JPanel getPanel_6() {
		if (panel_6 == null) {
			panel_6 = new JPanel();
			panel_6.setBounds(105, 145, 25, 10);
			panel_6.setPreferredSize(new Dimension(25, 10));
		}
		return panel_6;
	}

	private JTextField getText4() {
		if (text4 == null) {
			text4 = new JTextField();
			text4.setBounds(135, 135, 204, 30);
			text4.setPreferredSize(new Dimension(230, 30));
			text4.setMinimumSize(new Dimension(230, 21));
			text4.setMaximumSize(new Dimension(3000000, 2147483647));
			text4.setColumns(18);
		}
		return text4;
	}

	private JLabel getLabel_5() {
		if (label_5 == null) {
			label_5 = new JLabel("거래처");
			label_5.setBounds(35, 170, 65, 30);
			label_5.setForeground(Color.WHITE);
			label_5.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 22));
			label_5.setPreferredSize(new Dimension(65, 30));
			label_5.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return label_5;
	}

	private JPanel getPanel_7() {
		if (panel_7 == null) {
			panel_7 = new JPanel();
			panel_7.setBounds(105, 180, 25, 10);
			panel_7.setPreferredSize(new Dimension(25, 10));
		}
		return panel_7;
	}

	private JTextField getText5() {
		if (text5 == null) {
			text5 = new JTextField();
			text5.setBounds(135, 170, 204, 30);
			text5.setForeground(Color.BLUE);
			text5.setHorizontalAlignment(SwingConstants.CENTER);
			text5.setFont(new Font("맑은 고딕", Font.BOLD, 12));
			text5.setEditable(false);
			text5.setPreferredSize(new Dimension(230, 30));
			text5.setMinimumSize(new Dimension(230, 21));
			text5.setMaximumSize(new Dimension(3000000, 2147483647));
			text5.setColumns(18);
		}
		return text5;
	}

	public void input() {
		try {

			Statement stmt = con.createStatement();
			String code = text1.getText();
			String name = text2.getText();

			int price = Integer.parseInt(text3.getText());
			String mem = text4.getText();
			String customer = text5.getText();
			String sql = "insert into code values('" + code + "','" + name
					+ "'," + price + ",sysdate,'" + mem + "',sysdate,'" + mem
					+ "','" + customer + "')";

			ResultSet RS = stmt.executeQuery(sql);
			RS.close();
			stmt.close();
			text1.setText("");
			text2.setText("");
			text3.setText("");
			text4.setText("");
			text5.setText("");

			JOptionPane.showMessageDialog(null, "제품이 등록되었습니다.");
		} catch (Exception ev) {
			JOptionPane.showMessageDialog(null, ev.getMessage());
		}
	}

	private JSeparator getSeparator_6() {
		if (separator_6 == null) {
			separator_6 = new JSeparator();
			separator_6.setBounds(5, 5, 400, 5);
			separator_6.setPreferredSize(new Dimension(400, 5));
		}
		return separator_6;
	}

	private JPanel getPanel_8() {
		if (panel_8 == null) {
			panel_8 = new JPanel();
			panel_8.setBounds(5, 40, 25, 10);
			panel_8.setPreferredSize(new Dimension(25, 10));
		}
		return panel_8;
	}

	private JPanel getPanel_9() {
		if (panel_9 == null) {
			panel_9 = new JPanel();
			panel_9.setBounds(5, 75, 25, 10);
			panel_9.setPreferredSize(new Dimension(25, 10));
		}
		return panel_9;
	}

	private JPanel getPanel_11() {
		if (panel_11 == null) {
			panel_11 = new JPanel();
			panel_11.setBounds(5, 110, 25, 10);
			panel_11.setPreferredSize(new Dimension(25, 10));
		}
		return panel_11;
	}

	private JPanel getPanel_12() {
		if (panel_12 == null) {
			panel_12 = new JPanel();
			panel_12.setBounds(5, 145, 25, 10);
			panel_12.setPreferredSize(new Dimension(25, 10));
		}
		return panel_12;
	}

	private JPanel getPanel_13() {
		if (panel_13 == null) {
			panel_13 = new JPanel();
			panel_13.setBounds(5, 180, 25, 10);
			panel_13.setPreferredSize(new Dimension(25, 10));
		}
		return panel_13;
	}

	private JPanel getPanel_14() {
		if (panel_14 == null) {
			panel_14 = new JPanel();
			panel_14.setBounds(5, 218, 200, 3);
			panel_14.setBackground(Color.WHITE);
			panel_14.setPreferredSize(new Dimension(200, 3));
		}
		return panel_14;
	}

	private JPanel getPanel_15() {
		if (panel_15 == null) {
			panel_15 = new JPanel();
			panel_15.setPreferredSize(new Dimension(20, 10));
			panel_15.setBackground(Color.BLACK);
			panel_15.setLayout(null);
		}
		return panel_15;
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBackground(Color.BLACK);
			panel.setPreferredSize(new Dimension(300, 10));
			panel.setLayout(null);
			panel.add(getSeparator());
			panel.add(getPanel_10());
			panel.add(getText());
			panel.add(getButton());
			panel.add(getScrollPane());
			panel.add(getLabel_2());
		}
		return panel;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(10, 65, 280, 310);
			scrollPane.setPreferredSize(new Dimension(280, 310));
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					MouseC();
				}
			});
		}
		return table;
	}

	public void MouseC() {
		int idx = table.getSelectedRow();
		text5.setText(table.getValueAt(idx, 1) + "");
	}

	private JTextField getText() {
		if (text == null) {
			text = new JTextField();
			text.setBounds(10, 31, 182, 28);
			text.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_ENTER) {
						search2();
					}
				}

			});
			text.setPreferredSize(new Dimension(6, 28));
			text.setColumns(16);
		}
		return text;
	}

	private JButton getButton() {
		if (button == null) {
			button = new JButton("조 회");
			button.setBounds(197, 30, 93, 30);
			button.setForeground(Color.WHITE);
			button.setBackground(Color.BLACK);
			button.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 30));
			button.setPreferredSize(new Dimension(93, 30));
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					search2();
				}
			});
		}
		return button;
	}

	private JSeparator getSeparator() {
		if (separator == null) {
			separator = new JSeparator();
			separator.setBounds(-5, 5, 310, 5);
			separator.setPreferredSize(new Dimension(310, 5));
		}
		return separator;
	}

	private JPanel getPanel_4() {
		if (panel_4 == null) {
			panel_4 = new JPanel();
			panel_4.setBounds(5, 15, 340, 10);
			panel_4.setBackground(Color.BLACK);
			panel_4.setPreferredSize(new Dimension(340, 10));
			panel_4.setLayout(null);
		}
		return panel_4;
	}

	private JPanel getPanel_10() {
		if (panel_10 == null) {
			panel_10 = new JPanel();
			panel_10.setBounds(-10, 15, 320, 10);
			panel_10.setPreferredSize(new Dimension(320, 10));
			panel_10.setBackground(Color.BLACK);
			panel_10.setLayout(null);
		}
		return panel_10;
	}

	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("New label");
			lblNewLabel_1.setBounds(5, 240, 320, 150);
			lblNewLabel_1.setIcon(new ImageIcon("C:\\Users\\jhta\\Downloads\\db (2)\\라벨.jpg"));
			lblNewLabel_1.setPreferredSize(new Dimension(320, 150));
		}
		return lblNewLabel_1;
	}

	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("중앙HTA 할인매장 - 5조");
			label_2.setBounds(89, 405, 199, 29);
			label_2.setForeground(Color.WHITE);
			label_2.setFont(new Font("나눔손글씨 펜", Font.BOLD, 25));
		}
		return label_2;
	}
	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("입 력");
			button_1.setBounds(210, 205, 130, 30);
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					input();
				}
			});
			button_1.setPreferredSize(new Dimension(130, 30));
			button_1.setForeground(Color.WHITE);
			button_1.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 30));
			button_1.setBackground(Color.BLACK);
		}
		return button_1;
	}
}